package ru.rchervot.sanasar.client.view;

import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.HorizontalSplitPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;


public class ContentSearchResultsView extends Composite implements ContentSearchResultsPresenter.Display {
	
	private final ScrollPanel panelResults = new ScrollPanel();
	private final ScrollPanel panelRecord = new ScrollPanel();
	private final HorizontalPanel rightContentPanel = new HorizontalPanel();
	private final HorizontalSplitPanel basePanel = new HorizontalSplitPanel();
	
	private Presenter presenter;
	private Label startPageDisplay = new Label("Run search to see results here");
	private final Widget selectedNoneDisplay;
	private final Widget selectedSeveralDisplay;
	private final Widget recordDisplay;
	private int currentSectionsWidth = Constants.kSectionsWidthCollapsed;
	
	private int currentSplitPosition = Constants.RESULTS_SPLIT_POSITION;
	
	public ContentSearchResultsView(Widget resultsHeaderDisplay,
			Widget resultsDisplay,
			Widget resultsFooterDisplay,
			Widget recordHeaderDisplay,
			Widget recordDisplay,
			Widget recordFooterDisplay,
			Widget selectedNoneDisplay,
			Widget selectedSeveralDisplay) {
		
		this.recordDisplay = recordDisplay;
		this.selectedNoneDisplay = selectedNoneDisplay;
		this.selectedSeveralDisplay = selectedSeveralDisplay;
		
		recordDisplay.setVisible(false);
		selectedNoneDisplay.setVisible(false);
		selectedSeveralDisplay.setVisible(false);
		
		rightContentPanel.add(startPageDisplay);
		rightContentPanel.add(recordDisplay);
		rightContentPanel.add(selectedSeveralDisplay);
		rightContentPanel.add(selectedNoneDisplay);
		
		basePanel.setSize("100%", "100%");
		basePanel.setSplitPosition(currentSplitPosition + "%");
	  	
	  	panelResults.add(resultsDisplay);
	  	panelRecord.add(rightContentPanel);
	  	
	  	VerticalPanel left = new VerticalPanel();
	  	VerticalPanel right = new VerticalPanel();
	  	
	  	left.setSize("100%", "100%");
	  	right.setSize("100%", "100%");
	  	
	  	left.add(resultsHeaderDisplay);
	  	left.add(panelResults);
	  	left.add(resultsFooterDisplay);
	  	
	  	right.add(recordHeaderDisplay);
	  	right.add(panelRecord);
	  	right.add(recordFooterDisplay);
	  	
	  	basePanel.setRightWidget(right);
	  	basePanel.setLeftWidget(left);
		
		this.initWidget(basePanel);
		
	//window resize handler 
		Window.addResizeHandler(new ResizeHandler() { 
			public void onResize(ResizeEvent event) { 
				onWindowResized(Window.getClientWidth(), Window.getClientHeight());
			} 
		});
		
		panelRecord.setStylePrimaryName("RecordViewScrollPanel");
		panelResults.setStylePrimaryName("ResultsViewScrollPanel");
		
		onWindowResized(Window.getClientWidth(), Window.getClientHeight());
	}
	
	public void onWindowResized(int width, int height) {
		int splitHeight = height
		- Constants.kHeaderHeight
		- Constants.kModuleHeaderHeight
		- Constants.kMessagesHeight;
		
		basePanel.setHeight(splitHeight + "px");
		
		int splitWidth = width - (currentSectionsWidth + Constants.kSectionsWidthMargin);
		
		basePanel.setWidth(splitWidth + "px");
		
		int scrollerHeightResults = height
		- Constants.kHeaderHeight
		- Constants.kModuleHeaderHeight
		- Constants.kMessagesHeight
		- Constants.kResultsHeaderHeight
		- Constants.kResultsFooterHeight;
		
		if (scrollerHeightResults < 1) scrollerHeightResults = 1; //TODO 1px - really!?
		panelResults.setHeight(scrollerHeightResults + "px");
		
		int scrollerHeightRecord = height
		- Constants.kHeaderHeight
		- Constants.kModuleHeaderHeight
		- Constants.kMessagesHeight
		- Constants.kRecordHeaderHeight
		- Constants.kRecordFooterHeight;
		
		if (scrollerHeightRecord < 1) scrollerHeightRecord = 1; //TODO 1px - really!?
		panelRecord.setHeight(scrollerHeightRecord + "px");
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	
	
	@Override
	public void changeWidth(int sectionWidth) {
		currentSectionsWidth = sectionWidth;
		int newWidth = Window.getClientWidth() - (currentSectionsWidth + Constants.kSectionsWidthMargin);
		basePanel.setWidth(newWidth + "px");
	}
	
	@Override
	public void renderSelectedNone() {
		for (Widget w : rightContentPanel) {
			if (w == selectedNoneDisplay) {
				w.setVisible(true);
			} else {
				w.setVisible(false);
			}
		}
	}
	
	@Override
	public void renderSelectedSeveral() {
		for (Widget w : rightContentPanel) {
			if (w == selectedSeveralDisplay) {
				w.setVisible(true);
			} else {
				w.setVisible(false);
			}
		}
	}
	
	@Override
	public void renderRecord() {
		for (Widget w : rightContentPanel) {
			if (w == recordDisplay) {
				w.setVisible(true);
			} else {
				w.setVisible(false);
			}
		}
	}

	
	
	@Override
	public void changeSplitPosition(int splitPosition) {
		if (currentSplitPosition == 0 || currentSplitPosition == 100) {
			currentSplitPosition = Constants.RESULTS_SPLIT_POSITION;
		} else {
			currentSplitPosition = splitPosition;
		}
		
		basePanel.setSplitPosition(currentSplitPosition + "%");
	}
	
	@Override
	public void scrollToRowOffsetHeight(int rowOffsetHeight) {
		panelResults.setScrollPosition(rowOffsetHeight);
	}

	@Override
	public void scrollRecordToTop() {
		panelRecord.scrollToTop();
	}

	@Override
	public void renderStartPage() {
		for (Widget w : rightContentPanel) {
			if (w == startPageDisplay) {
				w.setVisible(true);
			} else {
				w.setVisible(false);
			}
		}
	}
	
}
